<?php
namespace Mall\Controller;

use Common\Controller\ApiController;
use Common\Model\ModelException;
use Util\ModelUtils;
use Util\Tree;

class YimeiActivityController extends ApiController
{
    
    public function activityList()
    {
        $activity=D('YimeiActivity')->scope('valid')->order('id desc')->limit(4)->select();
        $activity = transform('YimeiActivity','details',$activity);
        $this->response($activity);
    }

    public function goodsList()
    {
        $input = $this->request();
        $page = filter($input['page'],1);
        $perPage = filter($input['perPage'],10);
        $activity_id = filter($input['activity_id'],0);
        if($activity_id<=0){
            $activity = D('YimeiActivity')->scope('valid')->order('id desc')->find();
        }else{
            $activity = D('YimeiActivity')->scope('valid')->where(['id'=>$activity_id])->find();
        }
        if($activity){
            $activity = transform('YimeiActivity','details',$activity);
            $activity_goods=M('activityGoods')->where(['activity_id'=>$activity['id']])->select();
            if($activity_goods){
                $goods_ids = array_column($activity_goods,'goods_id');
                $count = D('Goods')->where(['goods_id'=>['in',$goods_ids]])->count();
                $goods = D('Goods')->where(['goods_id'=>['in',$goods_ids]])->page($page,$perPage)->select();
                $goods = transform('Goods', 'listview', $goods);
            }else{
                $goods=[];
                $count=0;
            } 
        }else{
            $this->errorResponse(90123);
        }
       
        $data['goods'] = $goods;
        $data['activity'] = $activity;
        $pagination = pagination($count, $perPage, $page);
        $this->response($data,$pagination);
    }

    private function fillGoods(&$goods)
    {
        $user = get_user();
        $attrs = D('GoodsAttr')->where(['goods_id' => $goods['goods_id'], 'status' => '1'])->select();
        $attrs = list_keep($attrs, ['id', 'name', 'val']);


        $specs = D('GoodsSpec')->where(['goods_id' => $goods['goods_id'], 'status' => '1'])->select();
        $specs = array_group_by($specs, 'name');
        foreach ($specs as &$spec) {
            $spec['items'] = list_keep($spec['items'], ['id', 'val']);
        }

        $skus = D('GoodsSku')->field('name,price,wholesale_price,stock')->where(['goods_id' => $goods['goods_id']])->select();
        // 售价判断
        foreach ($skus as &$sku) {
            $sku['price'] = $user['role'] === '2' ? $sku['wholesale_price'] : $sku['price'];
            unset($sku['wholesale_price']);
        }
        $goods['attrs'] = $attrs;
        $goods['specs'] = $specs;
        $goods['skus'] = $skus;
    }
    
    /*
     * 首页推荐活动模块
     */
     public function recommend()
    {
        $activity=D('YimeiActivity')
                ->where(['is_recommend'=>1,'delete_time'=>0,'is_closed'=>0])
                ->order('id desc')
                ->limit(10)
                ->select();
        $activity = transform('YimeiActivity','details',$activity);
        $this->response($activity);
    }
}
